package cn.lf.android.lawfirm.http.upload;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import cn.lf.android.lawfirm.utils.DebugLog;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
import okio.Okio;

import static android.content.ContentValues.TAG;

/**
 * Created by qianfan on 2017/8/31.
 */

public class UpLoadUtil {


    private void uploadMultiFile(String url, String name, String type, HashMap<String, Object> paramsMap) {
        File file = new File(name, type);
        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        MultipartBody.Builder builder = new MultipartBody.Builder();
//            //追加参数
        for (String key : paramsMap.keySet()) {
            Object object = paramsMap.get(key);
            if (!(object instanceof File)) {
                builder.addFormDataPart(key, object.toString());
            } else {
//                    File file = (File) object;
                builder.addFormDataPart(key, file.getName(), fileBody);
            }
        }
//        RequestBody body = builder.build();
        RequestBody requestBody = builder
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();


        final okhttp3.OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
        OkHttpClient okHttpClient = httpBuilder
                //设置超时
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "uploadMultiFile() e=" + e);
                DebugLog.e("上传失败");
            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i(TAG, "uploadMultiFile() response=" + response.body().string());
                DebugLog.e("上传失败");
            }
        });
    }


//
//    /**
//     *上传文件
//     * @param actionUrl 接口地址
//     * @param paramsMap 参数
//     * @param callBack 回调
//     * @param <T>
//     */
//    public <T> void upLoadFile(String actionUrl, HashMap<String, Object> paramsMap, final ReqProgressCallBack<T> callBack) {
//        try {
//            //补全请求地址
//            String requestUrl = String.format("%s/%s", upload_head, actionUrl);
//            MultipartBody.Builder builder = new MultipartBody.Builder();
//            //设置类型
//            builder.setType(MultipartBody.FORM);
//            //追加参数
//            for (String key : paramsMap.keySet()) {
//                Object object = paramsMap.get(key);
//                if (!(object instanceof File)) {
//                    builder.addFormDataPart(key, object.toString());
//                } else {
//                    File file = (File) object;
//                    builder.addFormDataPart(key, file.getName(), createProgressRequestBody(MEDIA_OBJECT_STREAM, file, callBack));
//                }
//            }
//            //创建RequestBody
//            RequestBody body = builder.build();
//            //创建Request
//            final Request request = new Request.Builder().url(requestUrl).post(body).build();
//            final Call call = mOkHttpClient.newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
//            call.enqueue(new Callback() {
//                @Override
//                public void onFailure(Call call, IOException e) {
//                    Log.e(TAG, e.toString());
//                    failedCallBack("上传失败", callBack);
//                }
//
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    if (response.isSuccessful()) {
//                        String string = response.body().string();
//                        Log.e(TAG, "response ----->" + string);
//                        successCallBack((T) string, callBack);
//                    } else {
//                        failedCallBack("上传失败", callBack);
//                    }
//                }
//            });
//        } catch (Exception e) {
//            Log.e(TAG, e.toString());
//        }
//    }
//
//
//    /**
//     *上传文件
//     * @param actionUrl 接口地址
//     * @param paramsMap 参数
//     * @param callBack 回调
//     * @param <T>
//     */
//    public <T>void upLoadFile(String actionUrl, HashMap<String, Object> paramsMap, final ReqCallBack<T> callBack) {
//        try {
//            //补全请求地址
//            String requestUrl = String.format("%s/%s", upload_head, actionUrl);
//            MultipartBody.Builder builder = new MultipartBody.Builder();
//            //设置类型
//            builder.setType(MultipartBody.FORM);
//            //追加参数
//            for (String key : paramsMap.keySet()) {
//                Object object = paramsMap.get(key);
//                if (!(object instanceof File)) {
//                    builder.addFormDataPart(key, object.toString());
//                } else {
//                    File file = (File) object;
//                    builder.addFormDataPart(key, file.getName(), RequestBody.create(null, file));
//                }
//            }
//            //创建RequestBody
//            RequestBody body = builder.build();
//            //创建Request
//            final Request request = new Request.Builder().url(requestUrl).post(body).build();
//            //单独设置参数 比如读取超时时间
//            final Call call = mOkHttpClient.newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
//            call.enqueue(new Callback() {
//                @Override
//                public void onFailure(Call call, IOException e) {
//                    Log.e(TAG, e.toString());
//                    failedCallBack("上传失败", callBack);
//                }
//
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    if (response.isSuccessful()) {
//                        String string = response.body().string();
//                        Log.e(TAG, "response ----->" + string);
//                        successCallBack((T) string, callBack);
//                    } else {
//                        failedCallBack("上传失败", callBack);
//                    }
//                }
//            });
//        } catch (Exception e) {
//            Log.e(TAG, e.toString());
//        }
//    }
//
//
//    /**
//     * 上传文件
//     * @param actionUrl 接口地址
//     * @param filePath  本地文件地址
//     */
//    public <T> void upLoadFile(String actionUrl, String filePath, final ReqCallBack<T> callBack) {
//        //补全请求地址
//        String requestUrl = String.format("%s/%s", BASE_URL, actionUrl);
//        //创建File
//        File file = new File(filePath);
//        //创建RequestBody
//        RequestBody body = RequestBody.create(MEDIA_OBJECT_STREAM, file);
//        //创建Request
//        final Request request = new Request.Builder().url(requestUrl).post(body).build();
//        final Call call = mOkHttpClient.newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.e(TAG, e.toString());
//                failedCallBack("上传失败", callBack);
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                if (response.isSuccessful()) {
//                    String string = response.body().string();
//                    Log.e(TAG, "response ----->" + string);
//                    successCallBack((T) string, callBack);
//                } else {
//                    failedCallBack("上传失败", callBack);
//                }
//            }
//        });
//    }
//
//
//    /**
//     * 创建带进度的RequestBody
//     * @param contentType MediaType
//     * @param file  准备上传的文件
//     * @param callBack 回调
//     * @param <T>
//     * @return
//     */
//    public <T> RequestBody createProgressRequestBody(final MediaType contentType, final File file, final ReqProgressCallBack<T> callBack) {
//        return new RequestBody() {
//            @Override
//            public MediaType contentType() {
//                return contentType;
//            }
//
//            @Override
//            public long contentLength() {
//                return file.length();
//            }
//
//            @Override
//            public void writeTo(BufferedSink sink) throws IOException {
//                Source source;
//                try {
//                    source = Okio.source(file);
//                    Buffer buf = new Buffer();
//                    long remaining = contentLength();
//                    long current = 0;
//                    for (long readCount; (readCount = source.read(buf, 2048)) != -1; ) {
//                        sink.write(buf, readCount);
//                        current += readCount;
//                        Log.e(TAG, "current------>" + current);
//                        progressCallBack(remaining, current, callBack);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        };
//    }

}
